package org.jeecg.modules.system.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.phone.entity.PhoneEntityVo;
import org.jeecg.modules.system.vo.ResultVo;
import org.springframework.web.util.HtmlUtils;

import java.util.regex.Pattern;

/**
 * @Description: 工具类XSSUtils，现在的做法是替换成空字符，CSDN的是进行转义，比如文字开头的"<"转成&lt;
 * @author: lsq
 * @date: 2021年07月26日 19:13
 */
public class ResultUtils {



    public static JSONObject ResultOkFromPage(IPage pageList ) {

        JSONObject joPageList = JSON.parseObject(JSON.toJSONString( pageList  ));
        joPageList.put("page", joPageList.getInteger("current"));
        joPageList.put("pageSize", joPageList.getInteger("size"));
        joPageList.put("rows", joPageList.getJSONArray("records"));

        joPageList.remove("current");
        joPageList.remove("size");
        joPageList.remove("records");

        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(ResultVo.OK(pageList)));
        jsonObject.put("data", joPageList);
        jsonObject.remove("result" );
        return jsonObject;
    }

    public static JSONObject ResultOk(String s) {


        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(ResultVo.OK(s)));
        jsonObject.put("data", s);
        jsonObject.remove("result" );
        return jsonObject;
    }

    public static JSONObject ResultErr(String s) {
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(ResultVo.error(s)));
        jsonObject.put("data", s);
        jsonObject.remove("result" );
        return jsonObject;
    }


    public static<T> ResultVo<T> OK(T data) {
        ResultVo<T> r = new ResultVo<T>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setData(data);
        return r;
    }


    public static<T> ResultVo<T> Err(T data) {
        ResultVo<T> r = new ResultVo<T>();
        r.setSuccess(false);
        r.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
        r.setData(data);
        return r;
    }
}
